/*
 * $Id$
 */

package corejs.rt;

import static corejs.rt.Int.*;

import java.math.BigDecimal;

/**
 *
 * @author pengyu
 */
public class Decimal extends Number {
   
   private BigDecimal _dec;

   public Decimal(final BigDecimal d) { _dec = d; }
   
   public Decimal(final int i) { _dec = new BigDecimal(i); }

   public Decimal(final long l) { _dec = new BigDecimal(l); }

   @Override
   public Bool toBool() {
      return _dec.equals(BigDecimal.ZERO) ? Bool.FALSE : Bool.TRUE;
   }

   @Override
   public Str toStr() { return new Str(_dec.toString()); }

   // --------------------------------------
   // add
   @Override
   public Number add(final Number n) { return n.add(_dec); }

   @Override
   protected Number add(final int i) { return add(new BigDecimal(i)); }

   @Override
   protected Number add(final BigDecimal d) { return new Decimal(_dec.add(d)); }

   @Override
   public Number add(final long l) { return add(new BigDecimal(l)); }

   // --------------------------------------
   // multiply
   @Override
   public Number multiply(final Number n) { return n.multiply(_dec); }

   @Override
   protected Number multiply(final int i) { return multiply(new BigDecimal(i)); }

   @Override
   protected Number multiply(final long l) { return multiply(new BigDecimal(l)); }

   @Override
   protected Number multiply(final BigDecimal d) {
      return new Decimal(_dec.multiply(d));
   }

   // --------------------------------------
   // substract
   @Override
   public Number subtract(final Number n) { return n.subtract(_dec); }

   @Override
   protected Number subtract(final int i) { return subtract(new BigDecimal(i)); }

   @Override
   protected Number subtract(final long l) { return subtract(new BigDecimal(l)); }

   @Override
   protected Number subtract(final BigDecimal d) {
      return new Decimal(d.subtract(_dec));
   }
   
   // --------------------------------------------
   // compareTo
   @Override
   public Number compareTo(final Number other) { return other.compareTo(_dec); }

   @Override
   Number compareTo(final int i) {return compareTo(new BigDecimal(i));}

   @Override
   Number compareTo(final long l) {return compareTo(new BigDecimal(l));}

   @Override
   Number compareTo(final BigDecimal d) {
      final int n = d.compareTo(_dec);
      return n == 0   ? ZERO
              : n < 0 ? MINUS_ONE
              :         ONE;
   }
   
   // ---------------------------------------------
   // equals
   //@Override
   @Override
   public boolean equals(final Number y) {return y.equals(_dec);}

   @Override
   public boolean equals(final int i) { return equals(new BigDecimal(i)); }

   @Override
   public boolean equals(final long l) { return equals(new BigDecimal(l)); }

   @Override
   public boolean equals(final BigDecimal d) {
      return compareTo(d) == ZERO;
   }

   // ---------------------------------------------
   @Override public long longValue() {return _dec.longValue();}

   @Override public int intValue() {return _dec.intValue();}
   
   @Override public Decimal neg() {return new Decimal(_dec.negate());}
}
